package com.neuedu.his.service;


import com.alibaba.fastjson.JSON;
import com.neuedu.his.common.Page;
import com.neuedu.his.common.constants.HisConstans;
import com.neuedu.his.common.utils.ParamUtils;
import com.neuedu.his.dao.*;
import com.neuedu.his.entity.Register;
import com.neuedu.his.common.core.BaseService;
import com.neuedu.his.entity.UmsUser;
import com.neuedu.his.vo.RegistVO;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.*;
import java.util.stream.Collectors;


public class RegisterService implements BaseService<Register> {

    private RegisterDao dao = new RegisterDao();
    private DeptDao deptDao = new DeptDao();
    private UmsUserDao userDao = new UmsUserDao();
    private RegistLevelDao registLevelDao = new RegistLevelDao();
    private CheckApplyDao checkApplyDao = new CheckApplyDao();
    private InspectApplyDao inspectApplyDao = new InspectApplyDao();


    public Page queryList(HttpServletRequest request) {
        int pageNo = 1;
        int size = 10;

        String paramPageNo = request.getParameter("pageNo");
        String paraSize = request.getParameter("size");

        if (!Objects.isNull(paramPageNo)) {
            pageNo = Integer.parseInt(paramPageNo);
        }
        if (!Objects.isNull(paraSize)) {
            size = Integer.parseInt(paraSize);
        }

        List paraList = new ArrayList();
        String where = "where 1=1  ";

        String registId = request.getParameter("registId");
        String name =  request.getParameter("name");

        String abc = "";
        String def = null;
        String qqq = "      ";


        if(!StringUtils.isBlank(registId)){
            where += " and regist_id = ? ";
            paraList.add(registId);
        }

        if (!StringUtils.isBlank(name)){
            where += "and name like concat('%',?,'%')";
            paraList.add(name);
        }

        where += "order by createtime desc ";

        Object[] params = paraList.toArray();
        long total = dao.selectCount(where, params);
        List<Register> data = dao.selectPage(where, pageNo, size, params);

        return Page.getInstatnce(pageNo, size, data, total);
    }


    public boolean save(HttpServletRequest req) {
        Register register = ParamUtils.getParam(Register.class, req);
        return dao.insert(register) > 0;
    }

    public boolean update(HttpServletRequest req) {
        Register register = ParamUtils.getParam(Register.class, req);
        return dao.updateById(register) > 0;
    }


    public Register queryById(HttpServletRequest req) {
        String registId = req.getParameter("registId");
        String where = " where regist_id = ?";
        Object params[] = {registId};
        return dao.selectOne(where, params);
    }


    /**
     * 根据主键删除
     *
     * @param req
     * @return
     */
    public boolean deleteById(HttpServletRequest req) {
        String registId = req.getParameter("registId");
        return dao.deleteById(Integer.parseInt(registId)) > 0;
    }

    public Map initEdit(HttpServletRequest request) {
        Map res = new HashMap();
        res.put("deptList", deptDao.selectList("where del = 1")); //
        res.put("levelList", registLevelDao.selectList("")); //没有 del createtime

        return res;
    }

    public List<UmsUser> queryDoctors(HttpServletRequest request) {
        String where = " where dept_id = ? and regist_level = ?";
        Object[] params = {
                request.getParameter("deptId"),
                request.getParameter("registLevel")
        };
        return userDao.selectList(where, params);
    }

    public Map queryRegistFeeItems(HttpServletRequest request) {
        Map map = new HashMap();
        String registId = request.getParameter("registId");

        Register register = dao.selectOne(" where regist_id = ?", registId);


        //已申请的检查  和检验项目
        List checkApplyItems = checkApplyDao.selectList(" where regist_id = ?", registId);
        List inspectApplyItems = inspectApplyDao.selectList(" where regist_id = ?", registId);

        map.put("register", register);
        map.put("checkApplyItems", checkApplyItems);
        map.put("inspectApplyItems", inspectApplyItems);

        return map;
    }

    /**
     *  收费
     * @param request
     * @return
     */
    public boolean fee(HttpServletRequest request) {
        String paramStr = request.getParameter("paramStr");
        String jsonStr = URLDecoder.decode(paramStr);


        RegistVO vo = JSON.parseObject(jsonStr, RegistVO.class);

        List<RegistVO.ApplyItem> selection = vo.getSelection();
        //检查项目
        //检查的项目
        List<Integer> checkApplys = selection.stream().
                filter(item -> {
                    return item.getType() == 1;
                }).map(item -> {
                    return item.getItemId();
                }).collect(Collectors.toList());


        int count1 = checkApplyDao.updateByCondation(HisConstans.CHECK_APPLY_PAY, checkApplys);

        //检验的项目
        List<Integer> inspectApplys = selection.stream().
                filter(item -> {
                    return item.getType() == 2;
                }).map(item -> {
                    return item.getItemId();
                }).collect(Collectors.toList());

        int count2 = inspectApplyDao.updateByCondation(HisConstans.CHECK_APPLY_PAY, inspectApplys);
        return count1 + count2 == selection.size();

    }

    /**
     * 退费
     * @param request
     * @return
     */
    public boolean refund(HttpServletRequest request) {
        String paramStr = request.getParameter("paramStr");
        String jsonStr = URLDecoder.decode(paramStr);


        RegistVO vo = JSON.parseObject(jsonStr, RegistVO.class);

        List<RegistVO.ApplyItem> selection = vo.getSelection();
        //检查项目
        //检查的项目
        List<Integer> checkApplys = selection.stream().
                filter(item -> {
                    return item.getType() == 1;
                }).map(item -> {
                    return item.getItemId();
                }).collect(Collectors.toList());


        int count1 = checkApplyDao.updateByCondation(HisConstans.CHECK_APPLY_REFUND, checkApplys);

        //检验的项目
        List<Integer> inspectApplys = selection.stream().
                filter(item -> {
                    return item.getType() == 2;
                }).map(item -> {
                    return item.getItemId();
                }).collect(Collectors.toList());

        int count2 = inspectApplyDao.updateByCondation(HisConstans.CHECK_APPLY_REFUND, inspectApplys);
        return count1 + count2 == selection.size();

    }

    /**
     * 退号
     * @param request
     * @return
     */
    public boolean handlerRefundNumber(HttpServletRequest request) {
        String registId = request.getParameter("registId");

        //根据主键查询，
        Register register = dao.selectOne(" where regist_id=  ? ", registId);

        //覆盖状态
        register.setStatus(HisConstans.REGISTER_Refund);

        //更新
        return dao.updateById(register)>0;

    }
}
